<template>
  <div class="my-container">
    <!-- 顶部区域 -->
    <div class="info-box">
      <!-- 头像 -->
      <van-image
        width="60"
        height="60"
        round
        fit="cover"
        :src="userInfo.photo"
      />
      <h2 class="name">
        {{ userInfo.name }}
        <br />
        <van-tag type="primary" color="#fff" text-color="#3296fa">{{
          userInfo.birthday
        }}</van-tag>
      </h2>
    </div>
    <!-- 操作链接布局 -->
    <van-row>
      <van-col span="8">
        <van-icon color="#77aaff" name="newspaper-o" /> <span>我的作品</span>
      </van-col>
      <van-col span="8">
        <van-icon color="#ff0000" name="star-o" /><span>我的收藏</span>
      </van-col>
      <van-col span="8">
        <van-icon color="#ffaa00" name="tosend" /><span>阅读历史</span>
      </van-col>
    </van-row>
    <!-- 底部的区域 -->
    <div class="bottom">
      <van-cell-group>
        <van-cell icon="edit" title="编辑资料" is-link to="/edit" />
        <van-cell icon="chat-o" title="小智同学" is-link />
        <van-cell icon="setting-o" title="系统设置" is-link />
        <van-cell @click="logout" icon="warning-o" title="退出登录" is-link />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
// 导入 api
// import { getUserInfo } from '../../api/my'
// 导入 token工具函数
import { removeToken } from '../../utils/token'
// 导入 mapState
import { mapState } from 'vuex'
export default {
  name: 'my',
  data () {
    return {
      // 用户信息
      // userInfo: {}
    }
  },
  created () {
    // 获取用户信息
    // getUserInfo().then(res => {
    //   // console.log('res:', res)
    //   this.userInfo = res.data.data
    // })
  },
  computed: mapState(['userInfo']),
  // computed: {
  //   // 简化 Vuex中的数据获取
  //   userInfo () {
  //     return this.$store.state.userInfo
  //   }
  // },
  methods: {
    logout () {
      this.$dialog
        .confirm({
          title: '你真的要退出了吗?嘤嘤嘤',
          message: '要不再想想看呗!o(╥﹏╥)o'
        })
        .then(() => {
          // 删除 token
          removeToken()
          // 删除用户信息
          // this.userInfo = {}
          this.$store.commit('setUserInfo', {})
          // 返回首页
          this.$router.push({ path: '/home' })
        })
        .catch(() => {
          // 取消执行的回调
          console.log('catch')
        })
    }
  }
}
</script>

<style lang="less">
.my-container {
  .info-box {
    height: 100px;
    background-color: #3296fa;
    display: flex;
    align-items: center;
    padding-left: 15px;
    .name {
      font-size: 14px;
      margin-left: 10px;
      color: white;
      font-weight: normal;
    }
  }
  .van-row {
    padding: 20px 0;
    // border-bottom: 1px solid #ccc;
    .van-col {
      text-align: center;
      font-size: 12px;
      .van-icon {
        font-size: 25px;
      }
      span {
        display: block;
      }
    }
  }
}
</style>
